﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AliCMS.TemplateEngine
{
    internal class Dict : Tag
    {
        internal Dict(Engine engine, int index, int length, int contentIndex, int contentLength, Tag parent)
            : base(engine, index, length, contentIndex, contentLength, parent)
        {
        }

        internal override void Parse()
        {
            ParseToCode();
            ParseToXml();
        }

        private void ParseToCode()
        {
            ResultCode = @"
                            Dictionary<string,string> _dict;
                            protected Dictionary<string,string> Dict
                            {
                                get
                                {
                                    if (_dict != null)
                                        return _dict;

                                    _dict = new Dictionary<string,string>();

                                    XmlNode dictNode = _xmlTemplate.SelectSingleNode(""//" + UniqueName + @"/Dict"");
                                    foreach (XmlNode node in dictNode.ChildNodes)
                                    {
                                        _dict[node.Name] = node.InnerText;
                                    }
                                    return _dict;
                                }
                            }
                        ";
        }
        private void ParseToXml()
        {
            ResultXml = "<Dict>" + Engine.Text.Substring(this.ContentIndex, this.ContentLength) + "</Dict>";
        }
    }
}
